package com.practices2.bean;

import java.util.HashMap;

import android.inputmethodservice.Keyboard.Key;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.practices2.R;
import com.practices2.common.TAStringUtils;

public class ViewHolderDownlaodList {

	public TextView tv_title;
	public ProgressBar progressBar;
	public TextView tv_speed;

	public Button btn_pause, btn_delete, btn_continue;

	private boolean hasInited = false;

	public  ViewHolderDownlaodList(View parentView) {
		if (parentView != null) {
			tv_title = (TextView) parentView.findViewById(R.id.title);
			tv_speed = (TextView) parentView.findViewById(R.id.speed);
			progressBar = (ProgressBar) parentView.findViewById(R.id.progress_bar);
			btn_pause = (Button) parentView.findViewById(R.id.btn_pause);
			btn_delete = (Button) parentView.findViewById(R.id.btn_delete);
			btn_continue = (Button) parentView.findViewById(R.id.btn_continue);
			hasInited = true;
		}
	}

	public static final int KEY_URL = 0;
	public static final int KEY_SPEED = 1;
	public static final int KEY_PROGRESS = 2;
	public static final int KEY_IS_PAUSED = 3;

	public static HashMap<Integer, String> getItemDataMap(String url, String speed, String progress, String isPause) {
		HashMap<Integer, String> map = new HashMap<Integer, String>();
		map.put(KEY_URL, url);
		map.put(KEY_SPEED, speed);
		map.put(KEY_PROGRESS, progress);
		map.put(KEY_IS_PAUSED, isPause);
		return map;
	}

	public void onPause() {
		if (hasInited) {
			btn_pause.setVisibility(View.GONE);
			btn_continue.setVisibility(View.VISIBLE);
		}
	}
	
	public void setData(HashMap<Integer, String> map){
		
		if (hasInited) {
			tv_title.setText(TAStringUtils.getFileNameFromUrl(map.get(KEY_URL)));
			tv_speed.setText(map.get(KEY_SPEED));
			String progress = map.get(KEY_PROGRESS);
			
			if (TextUtils.isEmpty(progress)) {
				progressBar.setProgress(0);
			}else{
				progressBar.setProgress(Integer.parseInt(progress));
			}
			if (Boolean.parseBoolean(map.get(KEY_IS_PAUSED))) {
				onPause();
			}
			
		}
	}
	
	public void setData(String url,String speed ,String progress){
		setData(url, speed, progress, false+"");
	}
	
	
	public void setData(String url,String speed ,String progress,String isPause){
		if (hasInited) {
			HashMap<Integer, String> map=getItemDataMap(url, speed, progress, isPause);
			
			tv_title.setText(TAStringUtils.getFileNameFromUrl(map.get(KEY_URL)));
			tv_speed.setText(speed);
			
			if (TextUtils.isEmpty(progress)) {
				progressBar.setProgress(0);
			}else{
				progressBar.setProgress(Integer.parseInt(progress));
			}
		}
	}

}
